package deibay.datamodel;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Vector;


public class Item implements Serializable{
	
	private Category category = null;
	private String name = null;
	private String id = null;
	private String description = null;
	private int startPrice;
	private int actualPrice;
	private String loginSeller = null;
	private Date finishTime = null;
	private String bidder = null;
	private Vector<String> subscribedUsers = null;
	
	public Item(String name, String description, String seller, Category cat, int price, Date finishTime)
	{
		this.name = name;
		this.description = description;
		this.loginSeller = seller;		
		this.category = cat;
		this.startPrice = price;
		this.actualPrice = 0;
		this.finishTime = finishTime;
		this.subscribedUsers = new Vector<String>();
		createID();
	}
	
	public Item(String name, String description, String seller, String bidder, Vector<String> subscribedUsers, String id, String cat, int price, int actualPrice, Date finishTime)
	{
		this.name = name;
		this.description = description;
		this.loginSeller = seller;
		this.bidder = bidder;
		this.subscribedUsers = subscribedUsers;
		this.id = id;
		this.startPrice = price;
		this.actualPrice = actualPrice;
		this.finishTime = finishTime;
		//falta tratamento da categoria..
	}
	
	private void createID() {
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			ByteArrayOutputStream arrStream = new ByteArrayOutputStream();
			ObjectOutputStream out;
			out = new ObjectOutputStream(arrStream);
			out.writeObject(Math.random());
			digest.update(arrStream.toByteArray());			
			id = System.currentTimeMillis() + getDigitString( digest.digest());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}			
	}

	private String getDigitString(byte[] digest)
	{
		StringBuffer buffer = new StringBuffer();		
		
		for (int i = 0; i < digest.length; i++)
		{
			buffer.append((new Byte(digest[i])).longValue());
		}
		return buffer.toString();
	}
	
	public int getStartPrice() {
		return startPrice;
	}
	
	public int getActualPrice()
	{
		return actualPrice;
	}
	
	public void setPrice(int price) {
		this.actualPrice = price;
	}
	
	public void setBidder(String bidder)
	{
		this.bidder = bidder;
	}
	
	public Category getCategory() {
		return category;
	}
	
	public String getDescription() {
		return description;
	}
	
	public String getId() {
		return id;
	}
	
	public String getName() {
		return name;
	}
	
	public String getSeller() {
		return loginSeller;
	}

	public String getBidder() {
		return bidder;
	}

	
	public Date getFinishTime() {
		return finishTime;
	}
	
	public Vector<String> getSubscribedUsers()
	{
		return this.subscribedUsers;
	}
	
	public String toString()
	{
		return this.name+" [id: "+id+"]";
	}
	
}
